#ifndef BATTLESYSTEM_H_
#define BATTLESYSTEM_H_

#include "subsystem.h"
#include "battlestats.h"
#include "entityfactory.h"
#include "battleentity.h"
#include "ui_battlemenu.h"
#include "c_lua_interface.h"

class CBattleSystem : public CSubSystem
{

private:
	
	CSmartVector< CQueuedBattleAttack > m_vBattleAttackQueue;
	
	std::vector< CBattleEntity * > m_vPlayers;
	std::vector< CBattleEntity * > m_vEnemies;
	
	CBattleEntity * m_pCurBattlePlayer;
	
	CBattleStats * m_pBattleStats;
	
	void DetermineBattlePos( int, CBattleEntity * );
	
	CFontMaterial * m_FontName, * m_FontReg, * m_FontSmall;
	CMaterial * m_MenuBoxMat, * m_BackgroundMat;
	
	CUIBattleMenu * m_BattleMenu;
	
	bool m_bStartBattle;
	bool m_bBattleDone;
	
	int m_BattleExitFunc, m_BattleThinkFunc;
	
	bool m_bTransitionDone;
	
	bool m_bBattleLocked;
	
	bool m_bSkipOuttroTransition;
	
	void FillCurrentPlayerMoveMenu();
	
public:
	
	CBattleSystem( CEntityFactory * e ) : CSubSystem( e ), m_FontName( NULL ), m_FontReg( NULL ), m_MenuBoxMat( NULL ), m_BattleMenu( NULL ), m_BackgroundMat( NULL )
	{
		
		m_pBattleStats = new CBattleStats;
		m_bStartBattle = false;
		m_bBattleDone = false;
		m_bSkipOuttroTransition = false;
		m_BattleThinkFunc = -1;
		m_BattleExitFunc = -1;
		
	}
	
	virtual ~CBattleSystem()
	{
		
		delete m_BattleMenu;
		
		delete m_pBattleStats;
	
		m_vBattleAttackQueue.clear();
		
	}
	
	inline void TurnoffOutro()
	{
		
		m_bSkipOuttroTransition = true;
		
	}
	
	inline void LockBattle()
	{
		
		m_bBattleLocked = true;
		
	}
	
	inline void UnlockBattle()
	{
		
		m_bBattleLocked = false;
		
	}
	
	inline void SetBattleThinkFunc( int f )
	{
		
		m_BattleThinkFunc = f;
		
	}
	
	inline void SetBattleExitFunc( int f )
	{
		
		m_BattleExitFunc = f;
		
	}
	
	void InitResources();
	
	inline CBattleEntity * GetBattlePlayer( int i )
	{
		
		return m_vPlayers[i];
		
	}
	
	inline void SetCurBattlePlayer( int i )
	{
		
		m_pCurBattlePlayer = m_vPlayers[i];
		
	}
	
	inline CBattleStats * GetBattleStats()
	{
		
		return m_pBattleStats;
		
	}
	
	void StartBattle();
	void EndBattle();
	
	inline bool BattleStarting()
	{
		
		return m_bStartBattle;
		
	}
	
	inline bool BattleEnding()
	{
		
		return m_bBattleDone;
		
	}
	
	void CleanBattle();
	
	void AttackQueueThink();
	
	void Input( SDL_Event &, Uint8 * );
	void Think();
	
	void DrawBackground();
	void DrawHUD();
	void DrawEntities();
	
	void GenerateHit( int, int, int );

	CBattleEntity * AddBattleEntity( int, std::string );
	
	void QueueAttack( CBattleEntity *, CBattleEntity *, CBattleAttackData * );

	void BindWithEntityFactory();
	
};

void SetBattleSystemEntityInterface( CEntityInterface * );

#endif
